# -*- coding: utf-8 -*-
'''
Created on 2022-04-06 14:11:06
@author: DengLibin 榆霖
@description: 爬虫 获取小说
'''
import time
import os
import requests
from bs4 import BeautifulSoup


##############################################
# @Author: DengLibin 榆霖
# @Date: Create in 2022-03-08 15:46:41
# @Description: 保存图片到文件
##############################################
def save_file(url):
    if not os.path.exists('files'):
        os.makedirs('files')
    file_name = url.split('/')[-1]
    file_resp = get_resp(url)
    with open('files/'+file_name, 'wb') as f:
        f.write(file_resp.content)
    file_resp.close()
    print('save file %s ok!' % file_name)




##############################################
# @Author: DengLibin 榆霖
# @Date: Create in 2022-03-08 15:56:43
# @Description: 获取响应
##############################################
def get_resp(url):
     # 设置请求头
    headers_dic = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36"
    }
    return requests.get(url, headers=headers_dic)

##############################################
# @Author: DengLibin 榆霖
# @Date: Create in 2022-03-08 14:44:08
# @Description: 获取网页内容
##############################################
def get_url_content(url) -> str:
   
    resp = get_resp(url)
    # 指定字符集
    resp.encoding = 'utf-8'
    page_content = resp.text
    resp.close()
    return page_content

##############################################
# @Author: DengLibin 榆霖
# @Date: Create in 2022-03-08 15:31:29
# @Description: 走起
##############################################
def run():
    for i in range(100):
        if i <= 2:
            continue;
        print("第", i, "页") 
        url = "https://www.zei8.vip/chuantongwuxia/index_%i.html" % i    
        content = get_url_content(url)
        # 1.解析数据
        page =BeautifulSoup(content, "html.parser")
        list_box = page.find(name = 'div', attrs={'class': 'listbox'})
        dl_list = list_box.find_all('li')
        for dl in dl_list:
            a = dl.find(name='a')
            detail = 'https://www.zei8.vip'+ a.get('href')
            content = get_url_content(detail)
            page =BeautifulSoup(content, "html.parser")
            strong = page.find(name = 'ul', attrs={'class':'downurllist'})
            a_s = strong.find_all('a')
            for aa in a_s:
                down_page = "https://www.zei8.vip" + aa.get('href')
                down_content = get_url_content(down_page)
                page =BeautifulSoup(down_content, "html.parser")
                down_span = page.find(name = 'span', attrs={'class': 'downfile'})
                down_a = down_span.find_all('a')
                for d_a in down_a:
                   save_file(d_a.get('href'))
                   break
                break
            time.sleep(1)
    
    
##############################################
# @Author: DengLibin 榆霖
# @Date: Create in 2022-03-08 15:31:42
# @Description: 程序入口
##############################################
if __name__ == '__main__':
    run()